package com.zl.awesome.current;

/**
 * @Author: zl
 * @Date: 2021/9/28 14:53
 */
public class NotifyAndNotifyAll {
    //create resource
    private static volatile Object resourceA = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                //get resourceA's monitor lock
                synchronized (resourceA){
                    System.out.println("threadA get resourceA lock");
                    try {
                        System.out.println("threadA begin wait");
                        resourceA.wait();
                        System.out.println("threadA end wait");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        //create thread
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                //get resourceA's monitor lock
                synchronized (resourceA){
                    System.out.println("threadB get resourceA lock");
                    try {
                        System.out.println("threadB begin wait");
                        resourceA.wait();
                        System.out.println("threadB end wait");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        //create thread
        Thread threadC = new Thread(new Runnable() {
            @Override
            public void run() {
                //get resourceA's monitor lock
                synchronized (resourceA){
                    System.out.println("threadC begin notify");
                    resourceA.notify();
                }
            }
        });
        //start thread
        threadA.start();
        threadB.start();
        Thread.sleep(1000);
        threadC.start();
        //wait thread over
        threadA.join();
        threadB.join();
        threadC.join();
        System.out.println("main over");
    }


}
